let app = getApp();

let base_url =app.base_url;
let obj = {
    base_url,
    token :app.common.token,
    get: function (params) {
        wx.showLoading({
            title: '加载中',
          })
        if(params.data){
            params.data.token = app.common.token
        }else{
            params.data = {
                token :app.common.token
            }
        }
        return new Promise(function (resolve, reject) {
            wx.request({
                url: base_url+params.url, //仅为示例，并非真实的接口地址
                data: params.data,
                method: 'GET',
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success(res) {
                    if(res.statusCode==200){
                        resolve(res.data)

                    }else{
                        wx.showToast({
                            title:'网络错误',
                            icon:'none'
                        })
                    }
                },
                fail(err) {
                    reject(err)
                },
                complete:function(){
                    wx.hideLoading()
                }
            })
        })
    }
    ,
    post: function (params) {
        wx.showLoading({
            title: '请稍后',
          })
        console.log(app.common.token)
        if(params.data){
            params.data.token = app.common.token
        }else{
            params.data = {
                token :app.common.token
            }
        }
        return new Promise(function (resolve, reject) {
            wx.request({
                url: base_url+params.url, //仅为示例，并非真实的接口地址
                data: params.data,
                method: 'POST',
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success(res) {
                    if(res.statusCode==200){
                        resolve(res.data)

                    }else{
                        wx.showToast({
                            title:'网络错误',
                            icon:'none'
                        })
                    }
                },
                fail(err) {
                    reject(err)
                },
                complete:function(){
                    wx.hideLoading()
                }
            })
        })
    },
    upload_img(num){
        if(!num){
            num = 1
        }
        console.log("upload_img");
        return new Promise(function(resolve,reject){
            wx.chooseImage({
                count: num,
                sizeType: ['original', 'compressed'],
                sourceType: ['album', 'camera'],
                success(res) {
                  // tempFilePath可以作为img标签的src属性显示图片
                  const tempFilePaths = res.tempFilePaths;
                
                  let for_is_over = false;
                  let img_arr = []
                  for(let i = 0;i<tempFilePaths.length;i++){
                      if(i==(tempFilePaths.length-1)){for_is_over=true}
                      wx.getImageInfo({
                          src: tempFilePaths[i],
                          success(img) {
                            console.log(img)
                            let img_type = img.type
                            let reader = wx.getFileSystemManager();
                            reader.readFile({
                              filePath: tempFilePaths[i],
                              encoding: "base64",
                              success: function (data) {
                                console.log(data);
                               
                                obj.post({
                                  url: 'upload/image',
                                  data: {
                                    token :app.common.token,
                                    image_base64:"data:image/"+img_type+";base64,"+ data.data
                                  }
                                }).then(res => {
                                    if(res.code==0){
                                        // console.log(res.data.image_url);
                                        img_arr.push(res.data.image_url);
                                        if(for_is_over){
                                            resolve(img_arr)
                                        }
                                    }else{
                                        app.toast(res.msg)
                                    }
                                  
                                 
                                })
                              }
                            })
                          }
                        })
                  }
                }
              })
        })
        
    },
    get_msg_code(type,phone){
        if(phone){
            return new Promise(function(resolve,reject){
                obj.get({
                    url:"send_sms",
                    data:{
                        type,
                        mobile:phone
                    }
                }).then(res=>{
                    if(res.code==0){
                        app.toast("验证码已发送");
                        resolve(true)
                    }else{
                        app.toast(res.message)
                    }
                })
            })
            
        }
        
    }
}
export default obj;